package com.chenxin.transaction.message;

import com.alibaba.fastjson.JSONObject;
import com.chenxin.transaction.dao.LisiAccountInfoDao;
import com.chenxin.transaction.entity.AccountChangeEvent;
import com.chenxin.transaction.service.AccountInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 消费者监听程序
 * @title: ProducerListener
 * @Author Star_Chen
 * @Date: 2022/6/1 9:33
 * @Version 1.0
 */
@Slf4j
@RocketMQMessageListener(consumerGroup = "consumer_group_txmsg_bank2",topic = "topic_txmsg")
@Component
public class LisiConsumer implements RocketMQListener<String> {

    @Autowired
    private AccountInfoService accountInfoService;

    //消费消息
    @Override
    public void onMessage(String message) {
        log.info("开始消费消息:{}",message);
        //解析消息
        JSONObject jsonObject = JSONObject.parseObject(message);
        String accountChangeString = jsonObject.getString("accountChange");
        //转成AccountChangeEvent
        AccountChangeEvent accountChangeEvent = JSONObject.parseObject(accountChangeString, AccountChangeEvent.class);
        //设置账号为李四的
        accountChangeEvent.setAccountNo("2");
        //更新本地账户，增加金额
        accountInfoService.doUpdateAccountBalance(accountChangeEvent);
    }
}
